import request from "@/utils/requset.js";
import { useAuthStore } from "@/store/modules/auth.js";
import { BASE_URL } from "@/utils/requset.js";
import {
  getStorage,
  setStorage,
  removeStorage,
} from "@/utils/storage/index.js";
export const file = {
  upload: (path, success, fail = () => {}) => {
    const authStore = useAuthStore();
    const token = authStore.getToken || getStorage('TOKEN')
    uni.uploadFile({
      url: BASE_URL + "/file/upload", //服务器地址
      // fileType: tempFiles[0].type, //ZFB必填,不然报错
      filePath: path, //这个就是我们上面拍照返回或者先中照片返回的数组
      name: "file",
      header: {
        authorization: `Bearer ${token}`,
      },
      success: (uploadFileRes) => {
        const { data } = uploadFileRes;
        const res = JSON.parse(data);
        const { code, msg } = res;
        if (code !== 200) {
          uni.showToast({
            title: msg,
            icon: "error",
          });
          return false;
        }
        success(res);
      },
      fail(err) {
        fail(err);
      },
    });
  },
  downLoadFile: (id) => {
    return request({
      method: "get",
      responseType: "blob",
      url: "/charge/downLoad/" + id,
    });
  },
  del: (id) => {
    return request({
      url: "/file/deleteById",
      method: "get",
      params: {
        id,
      },
    });
  },
  getFile: (url) => {
    return request({
      url: "/file/loadFile",
      method: "get",
      responseType: "blob",
      params: {
        url,
      },
    });
  },
};
